layui.define(['setter', 'table'], function (exports) {
    var $ = layui.jquery,
        setter = layui.setter,
        layer = layui.layer,
        local = layui.data(setter.tableName),
        container = $("#layui-body-content")
        //通用方法
        ,
        main = {
            //数据的异步请求 
            req: function (options) {
                parent.layui.admin.req(options);
            },
            headerToken: function () {                
                if (setter.request.tokenName) {
                    var local = layui.sessionData(setter.tableName);
                    if (local[setter.request.tokenName]) {
                        var tokeninfo = local[setter.request.tokenName];
                        return {
                            "Authorization": "Bearer " + tokeninfo.accessToken
                        }
                    }
                }
                return {}
            },
            headerTokenztzr: function () {
                // 主体责任token
                if (setter.request.tokenName) {
                    var local = layui.sessionData(setter.tableName + 'session');
                    if (local[setter.request.tokenName + 'ztzr']) {
                        var tokeninfo = JSON.parse(local['Authorizationztzr']);
                        return {
                            "Authorization": "Bearer " + tokeninfo.accesstoken
                        }
                    }
                }
                return {}
            },
            // 获取地址栏参数
            getPageParams(name) {
                var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
                var r = window.location.search.substring(1).match(reg);
                if (r != null) {
                    return decodeURI(r[2]);
                }
                return null;
            }

            //弹出面板
            ,
            popup: function (options) {
                return parent.layui.admin.popupChild(options);
            }
            //导出面板
            ,
            exportPopup: function (options) {
                return parent.layui.admin.popupExport(options);
            }, getPopHTML(data) {
                // var data = {
                //     isdisabled: true,
                //     formFilter: '',
                //     datafield:{ name: '名称', field: 'mc', type: 'text' },
                //     btnlist: [{name: '添加',filter: 'add'}]
                // }
                var htmlstr = `
                    <form class="layui-form" action="" lay-filter="${data.formFilter}">
                        <ul class="layui-form layui-form-pane formUl" style="margin: 15px;">
                    `
                if (data.datafield && data.datafield.length != 0) {
                    data.datafield.forEach(item => {
                        if (item.type == 'text') {
                            htmlstr += `
                            <li class="layui-form-item" ${item.width ? `style="width:${item.width}%"` : ''}>
                                <label class="layui-form-label">${item.name}</label>
                                <div class="layui-input-block">
                                    <input  lay-verify="required" class="layui-input" name="${item.field}" ${data.isdisabled ? 'disabled' : ''}>
                                </div>
                            </li>
                            `
                        } else if (item.type == 'date') {
                            htmlstr += `
                            <li class="layui-form-item">
                                <label class="layui-form-label">${item.name}</label>
                                <div class="layui-input-block">
                                    <input  lay-verify="required" type="text" name="${item.field}" 
                                    id="${item.id ? item.id : item.field}" lay-verify="date" autocomplete="off" class="layui-input"
                                     ${data.isdisabled ? 'disabled' : ''}>
                                </div>
                            </li>
                            `
                        } else if (item.type == 'textarea') {
                            htmlstr += `
                            <li class="layui-form-item" style="width:98%;">
                                <div class="layui-form-item layui-form-text">
                                    <label class="layui-form-label">${item.name}</label>
                                    <div class="layui-input-block">
                                        <textarea  lay-verify="required" class="layui-textarea" name="${item.field}" ${data.isdisabled ? 'disabled' : ''}></textarea>
                                    </div>
                                </div>
                            </li>`
                        } else if (item.type == 'select') {
                            htmlstr += `
                            <li class="layui-form-item"">
                                <label class="layui-form-label">${item.name}</label>
                                <div class="layui-input-block">
                                    <select name="${item.field}" ${data.isdisabled ? 'disabled' : ''} >
                                    <option value="">请选择</option>`

                            item.options.forEach(item => {
                                htmlstr += ` <option value="${item.value}">${item.name}</option>`
                            })
                            htmlstr += `</select></div></li>`
                        } else if (item.type == 'number') {
                            htmlstr += `
                            <li class="layui-form-item">
                                <label class="layui-form-label">${item.name}</label>
                                <div class="layui-input-block">
                                    <input type="number" name="${item.field}" lay-affix="number" min="${item.min ? item.min : 0}" max="${item.max ? item.max : '100'}"  ${data.isdisabled ? 'disabled' : ''} value="${item.default ? item.default : '0'}" class="layui-input">
                                </div>
                            </li>`
                        } else if (item.type == 'radio') {
                            if (Array.isArray(item.options) && item.options.length != 0) {
                                htmlstr += `
                                <li class="layui-form-item">
                                    <label class="layui-form-label">${item.name}</label>
                                    <div class="layui-input-block">`
                                item.options.forEach(function (a) {
                                    htmlstr += `<input type="radio" name="${item.name}" value="${a.value}" lay-filter="${a.filter ? a.filter : ''}" title="${a.title}"> `
                                })
                                htmlstr += ` </div>
                                </li>`
                            } else {
                                admin.msg_error(item.name + ':字段中options是不为空的数组！');
                            }
                        }
                    })
                }
                if (data.btnlist && data.btnlist.length != 0) {
                    htmlstr += ` <li class="layui-form-item" style="text-align:center; width:100%">`
                    data.btnlist.forEach(item => {
                        htmlstr += `<button type="button" lay-submit lay-filter="${item.filter}" class="layui-btn">${item.name}</button>`
                    });
                    htmlstr += `</li>`
                }
                htmlstr += `</ul></form>`;
                return htmlstr;
            },
            mainpopup: function (options) {
                var anims = 0,
                    caozuoBtn = [],
                    type = options.opentype,
                    size = options.size,
                    url = options.url,
                    layerArea;
                if (size === undefined || size === "auto" || size === "max") {
                    layerArea = ['80%', '80%'];
                } else if (size.indexOf(',') !== -1) {
                    let split = size.split(",");
                    layerArea = [split[0] + 'px', split[1] + 'px'];
                }
                //操作判断  
                if (options.caoZuo == 'add') {
                    caozuoBtn = ['添加', '关闭'];
                    anims = 1; //添加默认从上滑出
                } else if (options.caoZuo == 'search') {
                    caozuoBtn = ['立即查询', '关闭'];
                    anims = 2; //查询默认从下
                } else {
                    caozuoBtn = ['操作', '关闭'];
                    anims = Math.floor(Math.random() * 6 + 1);
                }
                // 动画判断 t:上方  r：右方  b：下方 random：随机
                if (type == 't') {
                    anims = 1;
                } else if (type == 'b') {
                    anims = 2;
                } else if (type == 'r') {
                    anims = 3;
                } else if (type == 'rr') {
                    anims = 4;
                } else if (type == 'x') {
                    anims = 5;
                } else if (type == 'd') {
                    anims = 6;
                } else if (type == 'random') {
                    anims = Math.floor(Math.random() * 6 + 1);
                }
                window.layerIndex = main.popup($.extend({
                    type: 2,
                    anim: anims,
                    closeBtn: 1,
                    maxmin: true,
                    area: layerArea,
                    content: [url, 'on'],
                    btn: caozuoBtn,
                    btnAlign: 'c',
                    success: function (layero, index) {
                        var containerDiv = layero.find('iframe').contents().find("#layui-body-content");
                        if (local['yj_FontSize']) {
                            containerDiv.addClass('layui-fontsize-' + local['yj_FontSize']);
                        }
                    },
                    cancel: function () {
                        layer.closeAll();
                    }
                }, options));
                if (size === "max") {
                    layer.full(layerIndex);
                }
            }
            //判断链接是否有效
            ,
            checkUrl: function (url) {
                layui.admin.checkUrl(url);
            }
            //成功提示
            ,
            msg_success: function (title) {
                layui.admin.msg_success(title);
            }
            //失败提示
            ,
            msg_error: function (title) {
                layui.admin.msg_error(title);
            }
            //……后续其他方法
        };
    //扩展 lib 目录下的其它模块
    layui.each(setter.extend, function (index, item) {
        var mods = {};
        mods[item] = '/html/main/config/lib/extend/' + item;
        layui.extend(mods);
    });

    exports('main', main);
});